<!DOCTYPE html>
<html>

<head>
    <title>文件转Base64</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        h1 {
            color: #333;
        }

        .container {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 5px;
            margin-top: 20px;
        }

        textarea {
            width: 100%;
            margin-top: 10px;
            padding: 10px;
            font-family: monospace;
        }

        .file-input {
            margin: 15px 0;
        }

        .info {
            color: #666;
            font-size: 14px;
            margin-top: 10px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>
    <h1>文件转Base64</h1>
    <div class="container">
        <div class="file-input">
            <label for="mediaFile">选择文件 (图片/音频/视频):</label><br>
            <input type="file" id="mediaFile" accept="image/*,audio/*,video/*">
        </div>

        <div class="info">
            <p>文件类型: <span id="fileType">未选择文件</span></p>
            <p>文件大小: <span id="fileSize">0</span> KB</p>
        </div>

        <textarea id="base64Output" rows="10" placeholder="Base64编码将显示在这里..."></textarea>

        <button id="copyButton">复制Base64</button>
    </div>

    <script>
        document.getElementById('mediaFile').addEventListener('change', function (e) {
            const file = e.target.files[0];
            if (!file) return;

            // 显示文件信息
            document.getElementById('fileType').textContent = file.type || '未知类型';
            document.getElementById('fileSize').textContent = (file.size / 1024).toFixed(2);

            const reader = new FileReader();
            reader.onloadend = function () {
                // 移除data:image/jpeg;base64,等前缀
                const base64String = reader.result.split(',')[1];
                document.getElementById('base64Output').value = base64String;
            }
            reader.readAsDataURL(file);
        });

        // 复制功能
        document.getElementById('copyButton').addEventListener('click', function () {
            const textarea = document.getElementById('base64Output');
            textarea.select();
            document.execCommand('copy');
            alert('Base64已复制到剪贴板');
        });
    </script>
</body>

</html>